package com.fr.data.core.db.dml;

import com.fr.base.FRContext;
import com.fr.data.core.db.dialect.Dialect;
import com.fr.data.core.db.dialect.DialectFactory;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Select extends AbstractDML
  implements Fragment
{
  private List selectColumnNameList;
  private Where where;

  public Select(Table paramTable)
  {
    this(paramTable, DialectFactory.getDefaultDialect());
  }

  public Select(Table paramTable, Dialect paramDialect)
  {
    this(paramTable, null, paramDialect);
  }

  public Select(Table paramTable, Where paramWhere, Dialect paramDialect)
  {
    super(paramTable, paramDialect);
    this.selectColumnNameList = new ArrayList();
    this.where = paramWhere;
  }

  public void addColumnName(String paramString)
  {
    if (paramString != null)
      this.selectColumnNameList.add(paramString);
  }

  public Where getWhere()
  {
    return this.where;
  }

  public void setWhere(Where paramWhere)
  {
    this.where = paramWhere;
  }

  public PreparedStatement createPreparedStatement(Connection paramConnection)
    throws SQLException
  {
    if (paramConnection == null)
    {
      FRContext.getLogger().log(Level.WARNING, "Connection can not be null.");
      return null;
    }
    if (getTable() == null)
    {
      FRContext.getLogger().log(Level.WARNING, "Table can not be null");
      return null;
    }
    if ((!(isEmptyWhereAllowed())) && (this.where == null))
    {
      FRContext.getLogger().log(Level.WARNING, "Where can not be null");
      return null;
    }
    String str = toStatementSQLString(this.dialect);
    FRContext.getLogger().log(Level.INFO, str.toString());
    PreparedStatement localPreparedStatement = paramConnection.prepareStatement(str);
    if (this.where != null)
      this.where.applyColumnTypeValue(this.dialect, paramConnection, getTable(), localPreparedStatement, 1);
    return localPreparedStatement;
  }

  public String noResultSQL()
  {
    if (getTable() == null)
      return null;
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(genTmpSelectSQLString());
    localStringBuffer.append(" where 1=2");
    return localStringBuffer.toString();
  }

  public String toStatementSQLString(Dialect paramDialect)
  {
    if (getTable() == null)
      return null;
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(genTmpSelectSQLString());
    if (this.where != null)
      localStringBuffer.append(this.where.toStatementSQLString(paramDialect));
    return localStringBuffer.toString();
  }

  public String toNormalSQLString(Dialect paramDialect)
  {
    if (getTable() == null)
      return null;
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(genTmpSelectSQLString());
    if (this.where != null)
      localStringBuffer.append(this.where.toNormalSQLString(paramDialect));
    return localStringBuffer.toString();
  }

  private String genTmpSelectSQLString()
  {
    String[] arrayOfString;
    int j;
    if (this.dialect == null)
      this.dialect = DialectFactory.getDefaultDialect();
    int i = this.selectColumnNameList.size();
    if (i == 0)
    {
      arrayOfString = new String[] { "*" };
    }
    else
    {
      arrayOfString = new String[i];
      for (j = 0; j < i; ++j)
        arrayOfString[j] = this.dialect.column2SQL((String)this.selectColumnNameList.get(j));
    }
    String str1 = getTable().toStatementSQLString(this.dialect);
    String str2 = "";
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(" ");
    int k = 0;
    int l = arrayOfString.length;
    while (k < l)
    {
      localStringBuffer.append(arrayOfString[k]);
      if (k < l - 1)
        localStringBuffer.append(",");
      ++k;
    }
    localStringBuffer.append(" ");
    str2 = localStringBuffer.toString();
    String str3 = "SELECT" + str2 + "FROM " + str1;
    return str3;
  }

  public static void main(String[] paramArrayOfString)
  {
    String[] arrayOfString;
    int j;
    ArrayList localArrayList = new ArrayList();
    Table localTable = new Table("James", "Table");
    Dialect localDialect = DialectFactory.getDefaultDialect();
    int i = localArrayList.size();
    if (i == 0)
    {
      arrayOfString = new String[] { "*" };
    }
    else
    {
      arrayOfString = new String[i];
      for (j = 0; j < i; ++j)
        arrayOfString[j] = localDialect.column2SQL((String)localArrayList.get(j));
    }
    String str1 = localTable.toStatementSQLString(localDialect);
    String str2 = "";
    StringBuffer localStringBuffer = new StringBuffer();
    localStringBuffer.append(" ");
    int k = 0;
    int l = arrayOfString.length;
    while (k < l)
    {
      localStringBuffer.append(arrayOfString[k]);
      if (k < l - 1)
        localStringBuffer.append(",");
      ++k;
    }
    localStringBuffer.append(" ");
    str2 = localStringBuffer.toString();
    String str3 = "SELECT" + str2 + "FROM " + str1;
    System.out.println(str3);
  }
}